﻿using System.Collections.Generic;
using System.Xml.Linq;

namespace FileZillaConfig.ConfigClasses
{
    public class Folders
    {
        private List<Folder> _list;

        public Folders()
        {
            this._list = new List<Folder>();
        }

        /// <summary>
        /// List of folders
        /// </summary>
        public List<Folder> List
        {
            get { return _list; }
            set { _list = value; }
        }

        /// <summary>
        /// Selects folder as home folder by its path
        /// </summary>
        /// <param name="path">Folder path</param>
        public void SetHomeFolder(string path)
        {
            foreach (Folder folder in this._list)
            {
                if (folder.Path != path)
                {
                    folder.IsHome = false;
                }
                else
                {
                    folder.IsHome = true;
                }
            }
        }

        /// <summary>
        /// Selects folder as home folder by its index
        /// </summary>
        /// <param name="index">Folder index</param>
        public void SetHomeFolder(int index)
        {
            int count = this._list.Count;
            for (int i = 0; i < count; i++)
            {
                if (i != index)
                {
                    this._list[i].IsHome = false;
                }
                else
                {
                    this._list[i].IsHome = true;
                }
            }
        }

        /// <summary>
        /// Get folder by path
        /// </summary>
        /// <param name="path">Folder path</param>
        /// <returns>Folder class</returns>
        public Folder GetFolder(string path)
        {
            foreach (Folder folder in this._list)
            {
                if (folder.Path == path)
                {
                    return folder;
                }
            }

            return null;
        }

        /// <summary>
        /// Get folder by index
        /// </summary>
        /// <param name="index">folder index</param>
        /// <returns>Folder class</returns>
        public Folder GetFolder(int index)
        {
            return this._list[index];
        }

        /// <summary>
        /// Remove first occurence of folder with specified path from list
        /// </summary>
        /// <param name="path">Folder path</param>
        /// <returns>True if removed</returns>
        public bool RemoveFolder(string path)
        {
            int count = this._list.Count;
            int index = -1;
            for (int i = 0; i < count; i++)
            {
                if (this._list[i].Path == path)
                {
                    index = i;
                    break;
                }
            }

            if (index > -1)
            {
                this._list.RemoveAt(index);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Remove folder from list by its index
        /// </summary>
        /// <param name="index">Folder index</param>
        /// <returns>True if removed</returns>
        public bool RemoveFolder(int index)
        {
            if ((this._list.Count > 0) && (this._list.Count > index) && (index >= 0))
            {
                this._list.RemoveAt(index);
                return true;
            }

            return false;
        }

        public XElement getXML()
        {
            XElement Permissions = new XElement("Permissions");

            foreach (Folder folder in this._list)
            {
                Permissions.Add(folder.getXML());
            }

            return Permissions;
        }


    }
}
